/*	
 *  Copyright (c) 2009-@year@. The GUITAR group at the University of Maryland. Names of owners of this group may
 *  be obtained by sending an e-mail to atif@cs.umd.edu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 *  documentation files (the "Software"), to deal in the Software without restriction, including without 
 *  limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 *	the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
 *	conditions:
 * 
 *	The above copyright notice and this permission notice shall be included in all copies or substantial 
 *	portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 *	LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
 *	EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 *	IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 *	THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */
package edu.umd.cs.imut.executor.replayer;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import edu.umd.cs.imut.exception.IException;
import edu.umd.cs.imut.executor.ExecutorTool;
import edu.umd.cs.imut.executor.IExecutor;
import edu.umd.cs.imut.executor.IExecutorMonitor;
import edu.umd.cs.imut.executor.WebMapUpdater;
import edu.umd.cs.imut.executor.replayer.IReplayer;
import edu.umd.cs.imut.model.IApplication;
import edu.umd.cs.imut.model.IIDGenerator;
import edu.umd.cs.imut.model.IO;
import edu.umd.cs.imut.model.WebApplication;
import edu.umd.cs.imut.model.WebDefaultIDGenerator;
import edu.umd.cs.imut.model.data.GUIMap;
import edu.umd.cs.imut.model.data.TestCase;

/**
 * Entry for Web replayer
 * <p>
 * 
 * @author <a href="mailto:baonn@cs.umd.edu"> Bao N. Nguyen </a>
 * 
 */
public class WebReplayerMain {

	/**
	 * 
	 */

	private static Logger LOGGER = Logger.getLogger(WebReplayerMain.class);;

	// Parameters for the replayer
	@Option(name = "-u", usage = "Root URL", aliases = "--url", required = true)
	String originURL = null;

	@Option(name = "-m", usage = "Map file to lookup components", aliases = "--map-file", required = true)
	String mapFile = null;

	@Option(name = "-t", usage = "Test case", aliases = "--testcase", required = true)
	String testcaseFile = null;

	@Option(name = "-om", usage = "Output map file to store new components", aliases = "--out-map-file", required = true)
	String outMapFile = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// PropertyConfigurator.configure(LOG4J_PROPERTIES);

		WebReplayerMain webReplayerMain = new WebReplayerMain();
		webReplayerMain.run(args);
	}

	/**
	 * @param args
	 * 
	 */
	private void run(String[] args) {

		CmdLineParser parser = new CmdLineParser(this);

		try {

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("START: " + this.getClass().getName());
			}

			parser.parseArgument(args);
			IApplication application = new WebApplication(originURL);

			// Configure the replayer
			GUIMap map = (GUIMap) IO.readObjFromFile(mapFile, GUIMap.class);
			TestCase testcase = (TestCase) IO.readObjFromFile(testcaseFile,
					TestCase.class);

			assert (testcase != null);
			assert (map != null);

			IExecutor executor = new WebReplayer(application, map, testcase);
			IReplayer replayer = (IReplayer) executor;
			ExecutorTool webReplayer = new ExecutorTool(replayer);

			IIDGenerator webIDGenerator = WebDefaultIDGenerator.getInstance();
			replayer.setIdGenerator(webIDGenerator);

			// IExecutorMonitor eventStateCollector = new EventStateCollector();
			// webReplayer.addMonitor(eventStateCollector);

			IExecutorMonitor mapUpdater = new WebMapUpdater(outMapFile);
			webReplayer.addMonitor(mapUpdater);

			// Execute
			webReplayer.execute();

			printInfo();

			LOGGER.info("DONE");

		} catch (CmdLineException e) {
			LOGGER.error(e.getMessage());
		} catch (IException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 */
	private void printInfo() {
		LOGGER.info("Output mapping: " + outMapFile);
	}
}
